//
//  WorkHomeVC.m
//  XLDoctor
//
//  Created by longma on 2018/7/27.
//  Copyright © 2018年 XLH. All rights reserved.
//mj设置登录密码

#import "SetLoginPasswordVC.h"
#import "LanguageController.h"
#import "CountryCodeVC.h"
#import "RegisterViewController.h"
#import "AllCountryCodeVC.h"
#import "NBPhoneNumberUtil.h"
#import "CaptchaView.h"
#import "EnterCodeController.h"
#import "MJConfirmLoadingView.h"
#import "SettingController.h"
#import "PersonalInfoController.h"
#import "AccountSecurityVC.h"

@interface SetLoginPasswordVC ()
@property (weak, nonatomic) IBOutlet UILabel *tipLabel;
@property (weak, nonatomic) IBOutlet MJConfirmLoadingView *confirmButton;
@property (weak, nonatomic) IBOutlet UIButton *encryptButton;
@property (weak, nonatomic) IBOutlet MJUITextField *passwordTextField;
@property (weak, nonatomic) IBOutlet UILabel *registTipLabel;

@end

@implementation SetLoginPasswordVC

- (void)viewDidLoad {
    [super viewDidLoad];
 
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
}

- (void)mj_setUI{
    [super mj_setUI];
    
    //
    //禁用返回手势 禁止左滑
    self.rt_disableInteractivePop = YES;
    [self.passwordTextField mj_becomeFirstResponder];
    self.passwordTextField.secureTextEntry = true;

    [self.encryptButton setImage:MJUIImageMake(@"icon_biyan_p") forState:0];
    [self.encryptButton setImage:MJUIImageMake(@"icon_zhengyan_p") forState:UIControlStateSelected];
    [self.confirmButton.continueButton setTitle:MJUILocalizedString(@"Done") forState:0];
    self.passwordTextField.mj_limitedNumber = 16;
    if(self.isRegister){
        self.registTipLabel.hidden = false;
    }else{
        self.registTipLabel.text = @"";
    }
    [self.btnBack setImage:[UIImage imageNamed:@"icon_shanchu"] forState:0];

    //
    MJWeakSelf
    self.confirmButton.handleContinueBlock = ^{
        [weakSelf handleConfirmButtonClick];

    };

    
    RAC(self.confirmButton, enabled) = [RACSignal combineLatest:@[self.passwordTextField.rac_textSignal] reduce:^id _Nullable(NSString * text){
        
        BOOL enable = false;
        if(text.length > 5){
            if(text.mj_containsUpperCaseLetters && text.mj_containsLowerCaseLetters && text.mj_containsDigits){
                enable = true;
            }
        }
        
        weakSelf.tipLabel.hidden = enable;
        
        return @(enable);
    }];
}

#pragma mark - 方法
- (IBAction)handleEncryptButtonClick:(UIButton *)sender {
    [self.encryptButton setSelected:!sender.isSelected];
    
    if(sender.isSelected){
        self.passwordTextField.secureTextEntry = false;
    }else{
        self.passwordTextField.secureTextEntry = true;
    }
}
- (void)handleBackButtonClick {
    [self.passwordTextField resignFirstResponder];
    MJWeakSelf
    MJUIAlertViewConfigure *configure = [[MJUIAlertViewConfigure alloc]init];
    configure.confirmButtonColor = UIColor.mj_appColor;
    configure.confirmTitle = MJUILocalizedString(@"Cancel_qx");
    configure.cancelTitle = MJUILocalizedString(@"Confirm Exit");
    configure.messageAlignmentCenter = true;
    NSString *messageString = MJUILocalizedString(@"Confirm exit? You can complete the setup of your login password by entering the settings later.");
    [MJUIAlertView alertViewWithTitle:MJUILocalizedString(@"Login Password Not Set_s") message:messageString configure:configure confirmBlock:^{
        
    } cancelBlock:^{
        if(weakSelf.isRegister){
            [weakSelf jumpNextController];
        }else{
            
            if([weakSelf.navigationController mj_containTargetVCWithClass:AccountSecurityVC.class]){
                [weakSelf.navigationController mj_popViewControllerWithTargetClass:AccountSecurityVC.class];
            }else{
                [weakSelf mj_popViewController];
            }
            
        }
    }];
}

- (void)handleConfirmButtonClick{
    
    [self.passwordTextField resignFirstResponder];

    [self.confirmButton.loadingView startAnimation];
    
    //忘记密码进来
    if(self.forgotPasswordParams){
        NSMutableDictionary *params = [NSMutableDictionary dictionaryWithDictionary:self.forgotPasswordParams];
        [params setValue:[HealthHelper aesEncryptString:self.passwordTextField.text] forKey:@"password"];
        
        NSString *url = user_forgotPassword;
        if([params mj_containsKey:@"email"]){
            url = user_forgotPasswordByEmail;
        }
        
        MJWeakSelf
        [MJHttpNetWorking requestWithParams:params requestUri:url requestType:RequestTypePut showLoading:false success:^(MJBaseModel *model) {
            
            [weakSelf.confirmButton.loadingView stopAnimation];

            if(model.code == 200){
                [RoyHud showToast:MJUILocalizedString(@"Setting successful")];
                MJUIAfter_GCD(MJUIAwaitTime, ^{
                    if(UserManager.shared.userModel){
                        [weakSelf.navigationController mj_popViewControllerWithTargetClass:SettingController.class];
                    }else{
                        [self.navigationController popToRootViewControllerAnimated:true];
//                        AppDelegate *appDelegate=(AppDelegate *)[[UIApplication sharedApplication]delegate];
//                        [appDelegate initRootViewController];
                    }
                });
             }

        } failure:^(NSError *error) {
            [weakSelf.confirmButton.loadingView stopAnimation];

        }];
    }else{
        NSString *password = [HealthHelper aesEncryptString:self.passwordTextField.text];
        MJWeakSelf
        [MJHttpNetWorking requestWithParams:@{@"password":password} requestUri:user_setPassword requestType:RequestTypePut showLoading:false success:^(MJBaseModel *model) {
            
            [weakSelf.confirmButton.loadingView stopAnimation];

            if(model.code == 200){
                [UserManager.shared saveUserInfo:model.data];
                [RoyHud showToast:MJUILocalizedString(@"Setting successful")];
                MJUIAfter_GCD(MJUIAwaitTime, ^{
                    
                    if(weakSelf.isRegister){
                        [weakSelf jumpNextController];
                    }else{
                        if([weakSelf.navigationController mj_containTargetVCWithClass:SettingController.class]){
                            [weakSelf.navigationController mj_popViewControllerWithTargetClass:SettingController.class];
                        }else{
                            [weakSelf mj_popViewController];
                        }
                    }
                    
                    
                });
             }

        } failure:^(NSError *error) {
            [weakSelf.confirmButton.loadingView stopAnimation];

        }];
    }
 

}
- (void)jumpNextController {
    UserModel *userModel = UserManager.shared.userModel;
    if(userModel.userName.length){
        AppDelegate *appDelegate=(AppDelegate *)[[UIApplication sharedApplication]delegate];
        [appDelegate initRootViewController];
//                            [RoyHud showToast:MJUILocalizedString(@"Log in successful")];

    }else{
        PersonalInfoController *vc = [[PersonalInfoController alloc]init];
        [self.navigationController pushViewController:vc animated:true];
//                            [RoyHud showToast:MJUILocalizedString(@"Sign up successful")];
    }
}

#pragma mark - 代理

#pragma mark - 懒加载

@end
